#include <iostream>

/*
虚继承
虚继承的基类叫做虚基类
虚基类的成员在被派生类继承时，只能继承一份，需要明确的初始化或者由默认构造函数
由虚继承指定的虚基类的成员在被派生类继承时，其内存布局将会移动到末尾，且在内存起始位置添加vbptr指针，指向vbtable，vbtable中存放的是
vbptr到内存起始位置的偏移，以及vbptr到这些成员的偏移
*/

#include <cstddef>
#include <cstdlib>
#include <iostream>

class Base {
  public:
    virtual void func() { std::cout << "Base::func()" << std::endl; }

    void operator delete(void *p) {
        std::cout << "Base::delete(void*), " << p << std::endl;
        free(p);
    }

    virtual ~Base() {}

  private:
    int _a;
};

class Derived : virtual public Base {
  public:
    void func() override { std::cout << "Derived::func()" << std::endl; }
    void *operator new(size_t size) {
        void *p = malloc(size);
        std::cout << "Derived::new(size_t), " << p << std::endl;
        return p;
    }

  private:
    int _b;
};

int main() {
    Base b;
    Derived d;

    std::cout << sizeof(b) << std::endl;
    std::cout << sizeof(d) << std::endl;
    std::cout << "===========================" << std::endl;

    Base *pd = new Derived;
    std::cout << "pd:" << pd << std::endl;

    pd->func();

    delete pd;
}
